package agents.behaviours;

import jade.core.behaviours.CyclicBehaviour;
import jade.core.behaviours.SimpleBehaviour;
import jade.domain.DFService;
import jade.domain.FIPAException;
import jade.domain.FIPANames;
import jade.domain.FIPAAgentManagement.DFAgentDescription;
import jade.domain.FIPAAgentManagement.ServiceDescription;
import jade.lang.acl.ACLMessage;
import jade.wrapper.ControllerException;

public class ClassifierBehaviour extends CyclicBehaviour {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private String attribute;

	public ClassifierBehaviour(String attribute){
		this.attribute = attribute;
	}
	
	
	@Override
	public void action() {
		DFAgentDescription query = new DFAgentDescription();
		ServiceDescription sd = new ServiceDescription();
		try {
			sd.setType(myAgent.getContainerController().getContainerName());
		} catch (ControllerException e) {
			throw new RuntimeException(e);
		}
		query.addServices(sd);
		try {
			DFAgentDescription[] result= DFService.search(myAgent, query);
			if(result.length!=0){
				ACLMessage msg = new ACLMessage(ACLMessage.INFORM_IF);
				msg.addReceiver(result[0].getName());
				msg.setContent(attribute);
				msg.setProtocol(FIPANames.InteractionProtocol.FIPA_QUERY);
				myAgent.send(msg);
				ACLMessage repl = myAgent.blockingReceive();
				System.out.println(repl);
			}else{
				block(1000);
			}
		} catch (FIPAException e1) {
			throw new RuntimeException(e1);
		}
		
		
		
	}

}
